package wu2020.top.spring.boot.flyway.simple.application.impl;

import com.wu.framework.database.lazy.web.plus.stereotype.LazyApplication;
import wu2020.top.spring.boot.flyway.simple.application.PersonApplication;
import org.springframework.web.bind.annotation.*;
import com.wu.framework.response.Result;
import com.wu.framework.response.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import wu2020.top.spring.boot.flyway.simple.domain.model.person.Person;
import wu2020.top.spring.boot.flyway.simple.application.command.person.PersonRemoveCommand;
import wu2020.top.spring.boot.flyway.simple.application.command.person.PersonStoryCommand;
import wu2020.top.spring.boot.flyway.simple.application.command.person.PersonUpdateCommand;
import wu2020.top.spring.boot.flyway.simple.application.command.person.PersonQueryListCommand;
import wu2020.top.spring.boot.flyway.simple.application.command.person.PersonQueryOneCommand;
import wu2020.top.spring.boot.flyway.simple.application.assembler.PersonDTOAssembler;
import wu2020.top.spring.boot.flyway.simple.application.dto.PersonDTO;
import java.util.stream.Collectors;
import jakarta.annotation.Resource;
import wu2020.top.spring.boot.flyway.simple.domain.model.person.PersonRepository;
import java.util.List;
import com.wu.framework.inner.lazy.database.expand.database.persistence.domain.LazyPage;
/**
 * describe person 
 *
 * @author Jia wei Wu
 * @date 2024/03/07 09:59 上午
 * @see com.wu.framework.inner.lazy.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl 
 **/
@LazyApplication
public class PersonApplicationImpl implements PersonApplication {

    @Resource
    PersonRepository personRepository;
    /**
     * describe 新增
     *
     * @param personStoryCommand 新增     
     * @return {@link Result<Person>} 新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<Person> story(PersonStoryCommand personStoryCommand) {
        Person person = PersonDTOAssembler.INSTANCE.toPerson(personStoryCommand);
        return personRepository.story(person);
    }
    /**
     * describe 批量新增
     *
     * @param personStoryCommandList 批量新增     
     * @return {@link Result<List<Person>>} 新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<List<Person>> batchStory(List<PersonStoryCommand> personStoryCommandList) {
        List<Person> personList = personStoryCommandList.stream().map( PersonDTOAssembler.INSTANCE::toPerson).collect(Collectors.toList());
        return personRepository.batchStory(personList);
    }
    /**
     * describe 更新
     *
     * @param personUpdateCommand 更新     
     * @return {@link Result<Person>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<Person> updateOne(PersonUpdateCommand personUpdateCommand) {
        Person person = PersonDTOAssembler.INSTANCE.toPerson(personUpdateCommand);
        return personRepository.story(person);
    }

    /**
     * describe 查询单个
     *
     * @param personQueryOneCommand 查询单个     
     * @return {@link Result<PersonDTO>} DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<PersonDTO> findOne(PersonQueryOneCommand personQueryOneCommand) {
        Person person = PersonDTOAssembler.INSTANCE.toPerson(personQueryOneCommand);
        return personRepository.findOne(person).convert(PersonDTOAssembler.INSTANCE::fromPerson);
    }

    /**
     * describe 查询多个
     *
     * @param personQueryListCommand 查询多个     
     * @return {@link Result<List<PersonDTO>>} DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<List<PersonDTO>> findList(PersonQueryListCommand personQueryListCommand) {
        Person person = PersonDTOAssembler.INSTANCE.toPerson(personQueryListCommand);
        return personRepository.findList(person)        .convert(persons -> persons.stream().map(PersonDTOAssembler.INSTANCE::fromPerson).collect(Collectors.toList())) ;
    }

    /**
     * describe 分页查询多个
     *
     * @param personQueryListCommand 分页查询多个     
     * @return {@link Result<LazyPage<PersonDTO>>} 分页DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<LazyPage<PersonDTO>> findPage(int size,int current,PersonQueryListCommand personQueryListCommand) {
        Person person = PersonDTOAssembler.INSTANCE.toPerson(personQueryListCommand);
        return personRepository.findPage(size,current,person)        .convert(page -> page.convert(PersonDTOAssembler.INSTANCE::fromPerson))            ;
    }

    /**
     * describe 删除
     *
     * @param personRemoveCommand 删除     
     * @return {@link Result<Person>}      
     
     * @author Jia wei Wu
     * @date 2024/03/07 09:59 上午
     **/

    @Override
    public Result<Person> remove(PersonRemoveCommand personRemoveCommand) {
     Person person = PersonDTOAssembler.INSTANCE.toPerson(personRemoveCommand);
     return personRepository.remove(person);
    }

}